package com.example.basefream.controller;


import com.example.basefream.service.InasyncService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Future;


@Api(tags = {"异步调用测试"})
@RestController
@RequestMapping("/testAsync")
public class AsyncController {

    @Autowired
    private InasyncService inasyncService;

    @RequestMapping("/test01")
    public String asyncTest() throws Exception {
        long start=System.currentTimeMillis();
        Future<String> taskOne=inasyncService.doTaskOne();
        Future<String> taskTwo=inasyncService.doTaskTwo();
        Future<String> taskThree=inasyncService.doTaskThree();
        Future<String> taskFour=inasyncService.doTaskFour();
        Future<String> taskFive=inasyncService.doTaskFive();
        Future<String> taskSix=inasyncService.doTaskSix();
        Future<String> taskSeven=inasyncService.doTaskSeven();
        Future<String> taskEight=inasyncService.doTaskEight();
        while(true) {
            if(taskOne.isDone() && taskTwo.isDone() && taskThree.isDone()&& taskFour.isDone()&&taskFive.isDone()&&taskSix.isDone()&&taskSeven.isDone()&&taskEight.isDone()) {
                // 八个任务都调用完成，退出循环等待
                break;
            }
            Thread.sleep(1000);
        }

        long end = System.currentTimeMillis();
        //八线程9030毫秒 自自定义是线程8040毫秒 自定义九线程7024毫秒
        System.out.println("任务全部完成，总耗时：" + (end - start) + "毫秒");

        return "";
    }
}
